﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using HslCommunication.BasicFramework;
using HslCommunication.LogNet;

namespace HslCommunication.Core.Net
{
	/// <summary>
	/// 带登录认证的服务器类，可以对连接的客户端进行筛选，放行用户名密码正确的连接<br />
	/// Server class with login authentication, which can filter connected clients and allow connections with correct username and password
	/// </summary>
	// Token: 0x02000136 RID: 310
	public class NetworkAuthenticationServerBase : NetworkServerBase, IDisposable
	{
		/// <summary>
		/// 当客户端的socket登录的时候额外检查的信息，检查当前会话的用户名和密码<br />
		/// Additional check information when the client's socket logs in, check the username and password of the current session
		/// </summary>
		/// <param name="socket">套接字</param>
		/// <param name="endPoint">终结点</param>
		/// <returns>验证的结果</returns>
		// Token: 0x060019DD RID: 6621 RVA: 0x00081224 File Offset: 0x0007F424
		protected override OperateResult SocketAcceptExtraCheck(Socket socket, IPEndPoint endPoint)
		{
			bool isUseAccountCertificate = this.IsUseAccountCertificate;
			if (isUseAccountCertificate)
			{
				OperateResult<byte[], byte[]> operateResult = base.ReceiveAndCheckBytes(socket, 2000);
				bool flag = !operateResult.IsSuccess;
				if (flag)
				{
					return new OperateResult(string.Format("Client login failed[{0}]", endPoint));
				}
				bool flag2 = BitConverter.ToInt32(operateResult.Content1, 0) != 5;
				if (flag2)
				{
					ILogNet logNet = base.LogNet;
					if (logNet != null)
					{
						logNet.WriteError(this.ToString(), StringResources.Language.NetClientAccountTimeout);
					}
					if (socket != null)
					{
						socket.Close();
					}
					return new OperateResult(string.Format("Authentication failed[{0}]", endPoint));
				}
				string[] array = HslProtocol.UnPackStringArrayFromByte(operateResult.Content2);
				string text = this.CheckAccountLegal(array);
				base.SendStringAndCheckReceive(socket, (text == "success") ? 1 : 0, new string[]
				{
					text
				});
				bool flag3 = text != "success";
				if (flag3)
				{
					return new OperateResult(string.Format("Client login failed[{0}]:{1} {2}", endPoint, text, SoftBasic.ArrayFormat<string>(array)));
				}
				ILogNet logNet2 = base.LogNet;
				if (logNet2 != null)
				{
					logNet2.WriteDebug(this.ToString(), string.Format("Account Login:{0} Endpoint:[{1}]", array[0], endPoint));
				}
			}
			return OperateResult.CreateSuccessResult();
		}

		/// <summary>
		/// 获取或设置是否对客户端启动账号认证<br />
		/// Gets or sets whether to enable account authentication on the client
		/// </summary>
		// Token: 0x17000561 RID: 1377
		// (get) Token: 0x060019DE RID: 6622 RVA: 0x00081367 File Offset: 0x0007F567
		// (set) Token: 0x060019DF RID: 6623 RVA: 0x0008136F File Offset: 0x0007F56F
		public bool IsUseAccountCertificate { get; set; }

		/// <summary>
		/// 新增账户，如果想要启动账户登录，必须将<see cref="P:HslCommunication.Core.Net.NetworkAuthenticationServerBase.IsUseAccountCertificate" />设置为<c>True</c>。<br />
		/// Add an account. If you want to activate account login, you must set <see cref="P:HslCommunication.Core.Net.NetworkAuthenticationServerBase.IsUseAccountCertificate" /> to <c> True </c>
		/// </summary>
		/// <param name="userName">账户名称</param>
		/// <param name="password">账户名称</param>
		// Token: 0x060019E0 RID: 6624 RVA: 0x00081378 File Offset: 0x0007F578
		public void AddAccount(string userName, string password)
		{
			bool flag = !string.IsNullOrEmpty(userName);
			if (flag)
			{
				this.lockLoginAccount.Enter();
				bool flag2 = this.accounts.ContainsKey(userName);
				if (flag2)
				{
					this.accounts[userName] = password;
				}
				else
				{
					this.accounts.Add(userName, password);
				}
				this.lockLoginAccount.Leave();
			}
		}

		/// <summary>
		/// 删除一个账户的信息<br />
		/// Delete an account's information
		/// </summary>
		/// <param name="userName">账户名称</param>
		// Token: 0x060019E1 RID: 6625 RVA: 0x000813E0 File Offset: 0x0007F5E0
		public void DeleteAccount(string userName)
		{
			this.lockLoginAccount.Enter();
			bool flag = this.accounts.ContainsKey(userName);
			if (flag)
			{
				this.accounts.Remove(userName);
			}
			this.lockLoginAccount.Leave();
		}

		// Token: 0x060019E2 RID: 6626 RVA: 0x00081428 File Offset: 0x0007F628
		private string CheckAccountLegal(string[] infos)
		{
			bool flag = infos != null && infos.Length < 2;
			string result;
			if (flag)
			{
				result = "User Name input wrong";
			}
			else
			{
				this.lockLoginAccount.Enter();
				bool flag2 = !this.accounts.ContainsKey(infos[0]);
				string text;
				if (flag2)
				{
					text = "User Name input wrong";
				}
				else
				{
					bool flag3 = this.accounts[infos[0]] != infos[1];
					if (flag3)
					{
						text = "Password is not corrent";
					}
					else
					{
						text = "success";
					}
				}
				this.lockLoginAccount.Leave();
				result = text;
			}
			return result;
		}

		/// <summary>
		/// 释放当前的对象
		/// </summary>
		/// <param name="disposing">是否托管对象</param>
		// Token: 0x060019E3 RID: 6627 RVA: 0x000814C0 File Offset: 0x0007F6C0
		protected virtual void Dispose(bool disposing)
		{
			bool flag = !this.disposedValue;
			if (flag)
			{
				if (disposing)
				{
					this.ServerClose();
					SimpleHybirdLock simpleHybirdLock = this.lockLoginAccount;
					if (simpleHybirdLock != null)
					{
						simpleHybirdLock.Dispose();
					}
				}
				this.disposedValue = true;
			}
		}

		/// <inheritdoc cref="M:System.IDisposable.Dispose" />
		// Token: 0x060019E4 RID: 6628 RVA: 0x00081504 File Offset: 0x0007F704
		public void Dispose()
		{
			this.Dispose(true);
		}

		/// <inheritdoc />
		// Token: 0x060019E5 RID: 6629 RVA: 0x0008150F File Offset: 0x0007F70F
		public override string ToString()
		{
			return string.Format("NetworkAuthenticationServerBase[{0}]", base.Port);
		}

		// Token: 0x040005DA RID: 1498
		private Dictionary<string, string> accounts = new Dictionary<string, string>();

		// Token: 0x040005DB RID: 1499
		private SimpleHybirdLock lockLoginAccount = new SimpleHybirdLock();

		// Token: 0x040005DC RID: 1500
		private bool disposedValue = false;
	}
}
